//
//  XYVideoView.m
//  XYRTCEngineSimpleDemo
//
//  Created by 高超 on 2021/12/3.
//

#import "XYVideoView.h"
#import "XYActivityIndicator.h"

@interface XYVideoView ()
@property (nonatomic, strong) UIView *micAndNameView;
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UIImageView *micImgView;
@property (nonatomic, strong) UILabel *displayNameLabel;
@property (nonatomic, strong) XYActivityIndicator *indicator;
@property (nonatomic, strong) UILabel *tipsLabel;

@end

@implementation XYVideoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.image = [UIImage imageNamed:@"onhold_normal_bg"];
        _bgImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self addSubview:_bgImageView];
        
        _micAndNameView = [[UIView alloc] init];
        _micAndNameView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        [self addSubview:_micAndNameView];
        
        _micImgView = [[UIImageView alloc] init];
        _micImgView.image = [UIImage imageNamed:@"calling_unmute_mic"];
        [_micAndNameView addSubview:_micImgView];
        
        _displayNameLabel = [[UILabel alloc] init];
        _displayNameLabel.font = [UIFont systemFontOfSize:10];
        _displayNameLabel.textColor = [UIColor whiteColor];
        _displayNameLabel.textAlignment = NSTextAlignmentCenter;
        [_micAndNameView addSubview:_displayNameLabel];
        
        _indicator = [[XYActivityIndicator alloc] init];
        _indicator.image = [UIImage imageNamed:@"waiting_loading"];
        [self addSubview:_indicator];
        
        _tipsLabel = [[UILabel alloc] init];
        [self addSubview:_tipsLabel];
        _tipsLabel.font = [UIFont systemFontOfSize:16];
        _tipsLabel.textColor = [UIColor whiteColor];
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGSize size = [_layout.remoteName boundingRectWithSize:CGSizeMake(0, 12) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]} context:nil].size;
    _bgImageView.frame = self.bounds;
    _micAndNameView.frame = CGRectMake(0, self.bounds.size.height-18, size.width+20, 18);
    _micImgView.frame = CGRectMake(3, 3, 11, 12);
    _displayNameLabel.frame = CGRectMake(17, 3, size.width, 12);
    _indicator.frame = CGRectMake(0, 0, 20, 20);
    _indicator.center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    _tipsLabel.frame = CGRectMake(0, 0, 100, 30);
    _tipsLabel.center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2 - 20);
}

- (void)setLayout:(NemoLayout *)layout {
    _layout = layout;
    _bgImageView.hidden = layout.layoutState==NemoStreamState_Received;
    _micImgView.image = layout.isAudioMute?[UIImage imageNamed:@"calling_mute_mic"]:[UIImage imageNamed:@"calling_unmute_mic"];
    _displayNameLabel.text = layout.remoteName;
    
    _tipsLabel.text = @"视频已关闭";
    _tipsLabel.hidden = layout.layoutState != NemoStreamState_Mute;
    
    if (layout.layoutState == NemoStreamState_Observing ||
        layout.layoutState == NemoStreamState_Idle ||
        layout.layoutState == NemoStreamState_NoBandwidth ||
        layout.layoutState == NemoStreamState_NoDecoder ||
        layout.layoutState == NemoStreamState_Mute) {
        [_indicator stopAnimating];
    } else if (layout.layoutState == NemoStreamState_Requesting) {
        [_indicator startAnimating];
    } else {
        [_indicator stopAnimating];
    }

}

@end
